package com.adcc.msgsimulator.service.impl;

import com.adcc.msgsimulator.bo.Msg620BO;
import com.adcc.msgsimulator.dto.Msg620Converter;
import com.adcc.msgsimulator.entity.Msg620DO;
import com.adcc.msgsimulator.mapper.Msg620Mapper;
import com.adcc.msgsimulator.service.Msg620Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * msg620服务层
 */
@Service
@Slf4j
public class Msg620ServiceImpl extends ServiceImpl<Msg620Mapper, Msg620DO> implements Msg620Service {

    @Resource
    private Msg620Converter msg620Converter;

    public void save(Msg620BO msg620) throws Exception {
        try {
            Msg620DO ldo = msg620Converter.toDO(msg620);
            //查询mysql，判断是否存在
            LambdaQueryWrapper<Msg620DO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(Msg620DO::getMsgID).eq(Msg620DO::getMsgID,ldo.getMsgID());
            List<Msg620DO> list = this.list(queryWrapper);
            //不存在插入
            if(list.size()==0){
                this.save(ldo);
            }
        } catch (Exception ex) {
            log.error("save() error", ex);
        }
    }

    @Override
    public boolean save(Msg620DO msg620) {
        LambdaQueryWrapper<Msg620DO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Msg620DO::getMsgID,msg620.getMsgID());
        List<Msg620DO> list = this.list(queryWrapper);
        //不存在插入
        if(list.size()==0){
            baseMapper.insert(msg620);
            return  true;
        }
        return false;
    }
}
